package FamousBallet;

import java.awt.*;
import javax.swing.*;

/**
 *<pre>
 File name  Splash.java
 * @author    <b><i>Yerong</i></b>
 * Instructor  Niko Culevski
 * Class       CS 141 B
 * Due date    11/29/2016
 * Assignment  Project_3
 * @version   1.0.1
 * Platform    PC, Windows 10, NetBeans 8.1
 *******************************************************************************
* This class makes the program shows a splash screen with a picture and a faked
* progress bar.
 *******************************************************************************
 * Input:    none
 * Output:   a faked progress bar
 *******************************************************************************
 * </pre>
 */
public class Splash extends JWindow {
    //instance variables

    private JProgressBar loading = new JProgressBar();
    private int duration;
    private int progress;

    //Constructor
    public Splash(int dur) {
        this.duration = dur;
    }

    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method showSplash()
     * Create a JPanel centered on the screen with a label showing an 
     * image and a progress bar and invokes a faked progress method.
     * @author Yerong
     * Date: 11/29/2016
     */     
    public void showSplash() {
        //Cast the contenter to JPanel
        JPanel content = (JPanel) getContentPane();
        content.setBackground(Color.lightGray);
        //Set the windows size and location
        int width = 2000, height = 1020;
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        //x,y indicate the location of the start point of the content JPanel.
        int x = (screen.width - width) / 2;
        int y = (screen.height - height) / 2;
        setBounds(x, y, width, height);

        //build the splash screen
        JLabel image = new JLabel(new ImageIcon("src/FamousBallet/title_anouncement.jpg"));
        JLabel info = new JLabel();
        info.setText("");
        info.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        info.setForeground(Color.blue);
        info.setFont(new java.awt.Font("Times New Roman", 0, 18));
        content.add(image, BorderLayout.CENTER);
        content.add(info,BorderLayout.NORTH);
        content.add(loading, BorderLayout.SOUTH);
        
        //Select border color and size
        Color border = new Color(50, 20, 20, 55);
        content.setBorder(BorderFactory.createLineBorder(border));

        //Display the JWindow
        this.setVisible(true);

        //Wait a little, while loading resources(fake)
        try {
            incProgress(100); //call a method to increment progress bar
            Thread.sleep(duration);
            this.dispose();
        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }
    
    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Method incProgress()
     * Create an object up with increamented progress duration, and 
     * invoke the myThread Thread to push the progress bar forward.
     * @param value, the little progress duration
     * @author Yerong
     * Date: 11/29/2016
     */   
    private void incProgress(int value) {
        //Instantiate an object from the inner class IncProgress and start the 
        //thread
        IncProgress up = new IncProgress(value);
        up.myThread.start();
    }

    //Inner class IncProgress
    /**
     * An inner class with a special thread to push the progress bar 
     * forward.
     * @author Yerong
     * @see java.lang.Thread   
     * Date: 11/29/2016 
     */
    class IncProgress {
        
        //An int variable determines how far does the progress bar goes
        private int howFar;
        
        //Constructor determines how far does the progress bar goes
        public IncProgress(int value) {
            howFar = value;
        }
        
        //Anonymous class IncProgress
        /**
         * An anonymous class pushes the progress bar.
         * @author Yerong
         * @see java.lang.Thread   
         * Date: 11/29/2016 
         */
        private Thread myThread = new Thread(new Runnable() {
            /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            * Method run()
            * A faked thread pushes the progress bar.
            * @author Yerong
            * Date: 11/29/2016
            */   
            public void run() {
                //Increment the progress bar
                while (progress < 2 * howFar) {
                    progress++;
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException exp) {
                        exp.printStackTrace();
                    }
                    loading.setValue(progress);
                }
            }
        });
    }
}
